package 栈和队列;

public class ArrayCircleQueue {
    private int front = 0;
    private int rear = 0;
    private int array[];
    private int capacity = 5;
    boolean flag = false;

    public ArrayCircleQueue() {
        array = new int[capacity];
    }

    public static ArrayCircleQueue initQueue(int[] arr) {
        ArrayCircleQueue queue = new ArrayCircleQueue();

        return queue;
    }

    public void enQueue(int data) {

        if (isFullQueue()) {
            try {
                throw new Exception("queue满");
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        this.rear = (this.rear + 1) % capacity;
        array[this.rear] = data;
        if (this.front == -1) {
            this.front = rear;
        }
        this.flag = false;


    }
    public void enQueueL(int data) {

        if ((rear+1)%capacity==front) {
            try {
                throw new Exception("queue满");
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        array[rear] = data;
        this.rear = (this.rear + 1) % capacity;



    }
    public int deQueueL() {
        if (rear == front) {
            try {
                throw new Exception("queue为空");
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        int val = array[this.front];
        this.front = (this.front + 1) % capacity;
        return val;
    }

    public void deQueue() {

        if (isEmpty()) {

            try {
                throw new Exception("queue为空");
            } catch (Exception e) {
                e.printStackTrace();
            }

        } else {
            int data = array[this.front];
            if (this.front == this.rear) {
                this.front = this.rear = -1;
            } else {
                this.front = (this.front + 1) % capacity;
            }

            System.out.println(data);
        }


    }


    public boolean isFullQueue() {
        return (rear + 1) % capacity == front;
    }

    public boolean isEmpty() {
        return front == -1 ;
    }

    private void doubleQueue() {
        int newArray[] = new int[capacity * 2];
        System.arraycopy(array, 0, newArray, 0, array.length);
        capacity = capacity * 2;
        array = newArray;
    }

    static class Main {
        public static void main(String[] args) {
            int[] a = {1, 3, 4, 5, 9};
            ArrayCircleQueue stack = ArrayCircleQueue.initQueue(a);
            for (int i : a) {
                stack.enQueueL(i);
            }

            //  stack.enQueue(0);
            /*  stack.enQueue(0); stack.enQueue(0);*/
          /*  stack.deQueue();
            stack.deQueue();
            stack.enQueue(0);
            stack.deQueue();
            stack.deQueue();
            stack.deQueue();
            stack.enQueue(0);
            stack.deQueue();
            stack.deQueue();*/
            stack.deQueueL();
            stack.deQueueL();
            stack.enQueueL(0);
            stack.deQueueL();
            stack.deQueueL();
            stack.deQueueL();

        }
    }
}
